package com.CTAtracker.android.Fragments;

import java.util.ArrayList;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.CTAtracker.android.R;
import com.CTAtracker.android.Activities.LineActivity;
import com.CTAtracker.android.Activities.SlidingActivity;
import com.CTAtracker.android.Adapters.LinesItemAdapter;
import com.CTAtracker.android.DAO.LineDataSource;
import com.CTAtracker.android.Models.Line;

/**
 * Fragment regarding the search of a line  
 */
public class SearchLineFragment extends Fragment {

	/**
	 * The list of Line 
	 */
	private ArrayList<Line> _listLine;
	/**
	 * The View of the list of Line
	 */
	private ListView _listViewLine;
	/**
	 * The attribute handling the connection to the database
	 */
	private LineDataSource _lineDataSource; 
	/**
	 * Listener on the list view. Retrieve the line selected and display the fragment station
	 */
	private OnItemClickListener mMessageClickedHandler = new OnItemClickListener() {
		public void onItemClick(AdapterView parent, View v, int position, long id) {
			Intent intent = new Intent((SlidingActivity)getActivity(), LineActivity.class);
			intent.putExtra("idLine", _listLine.get(position).get_id());
			startActivity(intent);
		}
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		_lineDataSource = new LineDataSource(container.getContext());
		return inflater.inflate(R.layout.searchline, container, false);
	}

	@Override
	public void onStart() {
		super.onStart();

		_lineDataSource.open();
		_listLine = (ArrayList<Line>) _lineDataSource.getAllLine(); 
		_lineDataSource.close();

		_listViewLine = (ListView) getView().findViewById(R.id.listViewTracks);
		LinesItemAdapter adapter = new LinesItemAdapter(getActivity(), R.layout.search_line_row, _listLine);
		_listViewLine.setAdapter(adapter);

		_listViewLine.setOnItemClickListener(mMessageClickedHandler);
	}
}
